import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // {
  //   path: '/spu-attrupdate',
  //   component: Layout,
  //   children: [{
  //     path: 'spu-attrupdate',
  //     name: 'spu-attrupdate',
  //     component: () => import('@/views/product/spuinfo/updateattr.vue'),
  //     meta: { title: '更新规格', icon: 'dashboard', hidden: true }
  //   }]
  // },

  {
    path: '/system',
    component: Layout,
    redirect: '/system/role',
    name: 'System',
    meta: { title: '权限管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色管理', icon: 'table' }
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user/index'),
        meta: { title: '用户管理', icon: 'tree' }
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/system/menu/index'),
        meta: { title: '菜单管理', icon: 'tree' }
      }
    ]
  },

  {
    path: '/product',
    component: Layout,
    name: '商品管理',
    redirect: '/product/list',
    meta: { title: '商品管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'list',
        name: '商品分类',
        component: () => import('@/views/product/category/index'),
        meta: { title: '商品分类', icon: 'form' }
      },
      {
        path: 'brand',
        name: '品牌管理',
        component: () => import('@/views/product/brand/index'),
        meta: { title: '品牌管理', icon: 'form' }
      },
      {
        path: '/attribute',
        component: () => import('@/components/menu'),
        redirect: '/attribute/group',
        name: '规格参数',
        meta: { title: '规格参数', icon: 'el-icon-s-help' },
        children: [
          {
            path: 'group',
            name: '属性分组',
            component: () => import('@/views/product/group/index'),
            meta: { title: '属性分组', icon: 'form' }
          },
          {
            path: 'attr',
            name: '基本属性',
            component: () => import('@/views/product/attribute/baseattr'),
            meta: { title: '属性列表', icon: 'form' }
          },
          {
            path: 'saleattr',
            name: '销售属性',
            component: () => import('@/views/product/attribute/saleattr'),
            meta: { title: '销售属性', icon: 'form' }
          },
        ]
      },
      {
        path: '/spu',
        component: () => import('@/components/menu'),
        redirect: '/spu/spuadd',
        name: '商品',
        meta: { title: '商品', icon: 'el-icon-s-help' },
        children: [
          {
            path: 'spuadd',
            name: '发布商品',
            component: () => import('@/views/product/spuinfo/spuadd'),
            meta: { title: '发布商品', icon: 'form' }
          },
          {
            path: 'spuinfo',
            name: 'spu管理',
            component: () => import('@/views/product/spuinfo/index'),
            meta: { title: 'spu管理', icon: 'form' }
          },
          {
            path: 'skuinfo',
            name: 'sku管理',
            component: () => import('@/views/product/skuinfo/index'),
            meta: { title: 'sku管理', icon: 'form' }
          },
          {
            path: 'waresku',
            name: '商品库存',
            component: () => import('@/views/product/ware/waresku'),
            meta: { title: '商品库存', icon: 'form' },
          },
        ]
      },
    ]
  },

  {
    path: '/user',
    component: Layout,
    redirect: '/user/list',
    name: 'User',
    meta: {
      title: '用户管理',
      icon: 'nested'
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/user/index'), // Parent router-view
        name: 'List',
        meta: { title: '用户列表' },
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
